#首先 import 必要的模块
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np


pd.set_option('display.max_columns',1000)
pd.set_option('display.width', 1000)
pd.set_option('display.max_colwidth',1000)

train = pd.read_csv("./FE_pima-indians-diabetes.csv")
print (train.head())
print("shape:", train.shape)

from sklearn.linear_model import LogisticRegression
lr = LogisticRegression(solver='liblinear')

##准备数据
y_train = train['Target']
X_train = train.drop(["Target"], axis=1)
print(X_train.head())
print("训练数据shape：",X_train.shape)
#保存特征名字以备后用（可视化）
feat_names = X_train.columns

# 交叉验证用于评估模型性能和进行参数调优（模型选择）
#分类任务中交叉验证缺省是采用StratifiedKFold
#数据集比较大，采用5折交叉验证
from sklearn.model_selection import cross_val_score
loss = cross_val_score(lr, X_train, y_train, cv=5, scoring='neg_log_loss')
#%timeit loss_sparse = cross_val_score(lr, X_train_sparse, y_train, cv=3, scoring='neg_log_loss')
print ('logloss of each fold is: ',loss)
print ('cv logloss is:', np.mean(loss))

loss = cross_val_score(lr, X_train, y_train, cv=5, scoring='accuracy')
#%timeit loss_sparse = cross_val_score(lr, X_train_sparse, y_train, cv=3, scoring='neg_log_loss')
print ('accuracy of each fold is: ',loss)
print ('cv accuracy is:', np.mean(loss))

#需要调优的参数
# 请尝试将L1正则和L2正则分开，并配合合适的优化求解算法（slover）
#tuned_parameters = {'penalty':['l1','l2'],
#                   'C': [0.001, 0.01, 0.1, 1, 10, 100, 1000]
#                   }
from sklearn.model_selection import GridSearchCV
penaltys = ['l1','l2']
Cs = [0.01, 0.1, 1, 10, 100]
tuned_parameters = dict(penalty = penaltys, C = Cs)
lr_penalty= LogisticRegression(solver='liblinear')
grid= GridSearchCV(lr_penalty, tuned_parameters,cv=5, scoring='neg_log_loss',n_jobs = -1,)
grid.fit(X_train,y_train)
print("LOG LOSS调参结果:")
print(grid.best_score_)
print(grid.best_params_)

grid= GridSearchCV(lr_penalty, tuned_parameters,cv=5, scoring='accuracy',n_jobs = -1,)
grid.fit(X_train,y_train)
print("accuracy 调参结果：")
print(grid.best_score_)
print(grid.best_params_)

